package com.eoner.baselibrary.utils;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.eoner.baselib.application.BaseApplication;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.common.config.Config;

import java.util.UUID;

/**
 * Created by Administrator on 2016/11/7.
 */
public class CookieNewUtil {

    public static String getCookie(Context context) {

        String token = SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN);
        return "platform=android;device=" + getDevicedId()+";token="+ token +";version="+getVersionName(context)+";deviceToken=" + Config.DEVICE_TOKEN+";device_id=" + Config.DEVICE_ID+Config.GZG_TEST_USER/*+";gzg_test_user=yaochun"*/;
    }

    public static String getVersionName(Context context) {
        try {
            String pkName = context.getPackageName();
            String versionName = context.getPackageManager().getPackageInfo(
                    pkName, 0).versionName;
            return versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    static String deviceId;
    public static String getDevicedId(){
        deviceId = getUniquePsuedoID();
        return deviceId;
    }

    //获得独一无二的Psuedo ID
    public static String getUniquePsuedoID() {
        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 位

        serial = Settings.Secure.getString(BaseApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        if(TextUtils.isEmpty(serial)){
            serial = SPUtils.getInstance().getString("android_id_key");
        }
        if(TextUtils.isEmpty(serial)){
            serial = UUID.randomUUID().toString();
            SPUtils.getInstance().put("android_id_key",serial);
        }
        //使用硬件信息拼凑出来的15位号码
        String uuid = new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        return uuid;
    }

}
